/**
 * @author Miguel Diaz//257835
 */
package dao;

import dao.exceptions.NonexistentEntityException;
import dao.exceptions.PreexistingEntityException;
import entity.BigSurfEstablishment;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;

/**
 * this class handle de CRUD operations in mongo db
 * for big surfaces establishments
 */

public class BigSurfEstablishmentMongoCtrl {

    private DBCollection dbcollection = null;

    public BigSurfEstablishmentMongoCtrl(DBCollection dbcollection) {
        this.dbcollection = dbcollection;
    }

    public DBCollection getDBCollection() {
        return dbcollection;
    }

    public void create(BigSurfEstablishment bigSurfEstablishment) throws PreexistingEntityException {

        DBCollection dbc = getDBCollection();
        BasicDBObject document = new BasicDBObject();
        document.put("name", bigSurfEstablishment.getNameEstablishment());
        document.put("nit", bigSurfEstablishment.getNit());
        document.put("CEOFirstName", bigSurfEstablishment.getCEOFirstName());
        document.put("CEOSecondName", bigSurfEstablishment.getCEOSecondName());
        document.put("CEOId", bigSurfEstablishment.getCEOId());
        document.put("CEOId", bigSurfEstablishment.getEmail());
        //validations of unique fields must be inserted here!
        if (dbc.findOne(document) == null) {
            dbc.insert(document);
        } else {
            throw new PreexistingEntityException("The big surface establishment with name "
                    + bigSurfEstablishment.getNameEstablishment() + "couldn't be created. Already exist.");
        }
    }

    public void edit(BigSurfEstablishment oldBigSurf, BigSurfEstablishment newBigSurf) throws NonexistentEntityException, Exception {
        DBCollection dbc = null;
        BasicDBObject old = new BasicDBObject();
        BasicDBObject newco = new BasicDBObject();

        try {
            dbc = getDBCollection();

            old.put("name", oldBigSurf.getNameEstablishment());
            old.put("nit", oldBigSurf.getNit());
            old.put("CEOFirstName", oldBigSurf.getCEOFirstName());
            old.put("CEOSecondName", oldBigSurf.getCEOSecondName());
            old.put("CEOId", oldBigSurf.getCEOId());
            old.put("CEOId", oldBigSurf.getEmail());


            newco.put("name", newBigSurf.getNameEstablishment());
            newco.put("nit", newBigSurf.getNit());
            newco.put("CEOFirstName", newBigSurf.getCEOFirstName());
            newco.put("CEOSecondName", newBigSurf.getCEOSecondName());
            newco.put("CEOId", newBigSurf.getCEOId());
            newco.put("CEOId", newBigSurf.getEmail());

            dbc.update(dbc.findOne(old), newco);
        } catch (Exception ex) {
            if (ex.getLocalizedMessage().equals("can't save a null object")) {
                throw new NonexistentEntityException("The big surface establishment with name"
                        + oldBigSurf.getNameEstablishment() + "couldn't be updated. May no longer exist");
            }
            throw ex;

        }

    }

    public void destroy(BigSurfEstablishment bigSurfEstablishment) throws NonexistentEntityException {
        DBCollection dbc = null;
        BasicDBObject todestroy = new BasicDBObject();

        try {
            dbc = getDBCollection();
            todestroy.put("name", bigSurfEstablishment.getNameEstablishment());
            todestroy.put("nit", bigSurfEstablishment.getNit());
            todestroy.put("CEOFirstName", bigSurfEstablishment.getCEOFirstName());
            todestroy.put("CEOSecondName", bigSurfEstablishment.getCEOSecondName());
            todestroy.put("CEOId", bigSurfEstablishment.getCEOId());
            todestroy.put("CEOId", bigSurfEstablishment.getEmail());

            dbc.remove(dbc.findOne(todestroy));
        } catch (Exception enfe) {
            throw new NonexistentEntityException("The big surface establishment with name "
                    + bigSurfEstablishment.getNameEstablishment() + "couldn't be deleted. May no longer exist.");
        }
    }

    public void findAdministratorEntities() {
        DBCollection dbc = getDBCollection();
        DBCursor cursor = dbc.find();

        // loop over the cursor and display the retrieved result
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }


    }
}
